﻿
//A simple algorithm
inline void GregorianToLunar(const SolarTime& date, LunarTime& lun) noexcept
{
	assert( date.IsValid() && date.Year() >= 1901 );
	//year - 1901 = 4Q + R
	int32_t r = date.Year() - 1901;
	int32_t q = r / 4;
	r = r - q * 4;
	//day = 14Q+10.6*(R+1)+ordinal_day - 29.5n
	int32_t d = 140 * q + 106 * (r + 1) + (SolarTimeHelper::CalcDayOrdinal(date.Year(), date.Month(), date.Day()) + 1) * 10;
	q = d / 295;
	r = d - q * 295;
	lun.Day() = r / 10;
	lun.Year() = date.Year();
	q = q / 10 - 2;
	if ( q < 0 ) {
		-- (lun.Year());
		q += 12;
	}
	lun.Month() = q + 1;
	lun.MonthFlags() = 0;
	//1901-2-19 = 1901-1-1(lunar)
}
